/*
 */

package labeledbox;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;


/**
 */
class AnimateBox implements ActionListener {

  LabeledBox selectedLabeledBox;
  int x_from, y_from, x_to, y_to;
  int x_dist, y_dist;           // distance from x_from to x_to and y_from to y_to
  int tot_animation_frames;
  int num_frames_processed = 0;
  DrawPanel drawPanel;
  
  Timer timer;

  //// end Class Vars ////
  ////////////////////////

  // Constructor
  public AnimateBox( LabeledBox selectedLabeledBox, int left, int top,
                     int tot_animation_frames, DrawPanel drawPanel     ) {

    this.selectedLabeledBox = selectedLabeledBox;
    this.x_to = left;
    this.y_to = top;
    this.tot_animation_frames = tot_animation_frames;
    this.drawPanel = drawPanel;

    this.x_from = selectedLabeledBox.getLeft();
    this.y_from = selectedLabeledBox.getTop();
    //this.x_dist = Math.abs(x_from - x_to);
    //this.y_dist = Math.abs(y_from - y_to);

    this.x_dist = x_to - x_from;
    this.y_dist = y_to - y_from;

  } ///// end constructor //////////

  
  public void setTimer(Timer timer) {
    this.timer = timer;
  }

  public void actionPerformed(ActionEvent e) {

    num_frames_processed++;

    double frac_frames_done = (double) num_frames_processed / tot_animation_frames;

    int new_left = (int) (x_from + frac_frames_done * x_dist);
    int new_top  = (int) (y_from + frac_frames_done * y_dist);

    selectedLabeledBox.setLeft(new_left);
    selectedLabeledBox.setTop(new_top);

    drawPanel.repaint();

    if(num_frames_processed == tot_animation_frames) {
      timer.stop();
    }
    
  } //// end actionPeformed() ////////

  
} //////////////////// end class AnimateBox /////////////////////////////
